home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / JOYSTICK.SWG / 0005_JOYSTCK5.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  5KB  |  207 lines

  1. {
  2. to whomever sent me a message concerning joystick support, I apologize that I
  3. cannot send this message to you directly (message Pointers were screwed up on m
  4. end, and I lost your message), but here is both my source For a Unit and a
  5. sample Program.  First I'd like to say that my Unit may be somewhat inComplete.
  6. have only a Single joystick port, so reading of two ports is impossible.  For
  7. this reason, I'd like to ask any and all to make suggestions, and modifications
  8. so that I, and all Programmers, may have a Complete Unit.  Also, remarks have
  9. not been added to the Program, if an explanation is needed, please feel free to
  10. ask...I'd be more than happy to give explanations For my work.  Anyhows, here i
  11. is...
  12. }
  13.  
  14. Unit Joystick;
  15.  
  16. Interface
  17.  
  18. Function JoystickExists : Boolean;
  19. Function JoystickPosX : Integer;
  20. Function JoystickPosY : Integer;
  21. Function JoystickButtonA : Boolean;
  22. Function JoystickButtonB : Boolean;
  23.  
  24. Implementation
  25.  
  26. Uses Crt, Dos;
  27.  
  28. Const GamePortAddr = $200;
  29.      MaxCount = 500;
  30.  
  31. Function JoystickStatus (Mask : Byte) : Integer;
  32. Var Counter : Integer;
  33. Label Read;
  34. begin
  35.   Asm
  36.   mov cx,MaxCount
  37.   mov dx,GamePortAddr
  38.   mov ah,Mask
  39.   out dx,al
  40.   read:
  41.      in al,dx
  42.      test al,ah
  43.      loopnz read
  44.   mov counter,cx
  45.   end;
  46.   JoystickStatus := MaxCount - Counter;
  47.   Delay (2);
  48. end;
  49.  
  50. Function JoystickPosX : Integer;
  51. begin
  52.   JoystickPosX := JoystickStatus (1);
  53. end;
  54.  
  55. Function JoystickPosY : Integer;
  56. begin
  57.   JoystickPosY := JoystickStatus (2);
  58. end;
  59.  
  60. Function JoystickButtonA : Boolean;
  61. begin
  62.   JoystickButtonA := (Port [GamePortAddr] and 16) = 0;
  63. end;
  64.  
  65. Function JoystickButtonB : Boolean;
  66. begin
  67.   JoystickButtonB := (Port [GamePortAddr] and 32) = 0;
  68. end;
  69.  
  70. Function JoystickExists : Boolean;
  71. Var Regs : Registers;
  72. begin
  73.   JoystickExists := not ((JoystickPosX = 0) and (JoystickPosY = 0));
  74. end;
  75.  
  76. end.
  77.  
  78.  
  79. {
  80.  
  81. Program JoyTest;
  82.  
  83. Uses Crt, Dos, AniVGA, Joystick;
  84.  
  85. Var XMin, XMax, YMin, YMax,
  86.    XRange, YRange,
  87.    X, Y,
  88.    PosX, PosY,
  89.    Bullet1X, Bullet1Y,
  90.    Bullet2X, Bullet2Y : Integer;
  91.    Shooting1, Shooting2 : Boolean;
  92.    ShootNext : Boolean;
  93.  
  94. Procedure CalibrateJoystick (Var XMin, XMax, YMin, YMax : Integer);
  95. begin
  96.   Write ('Press joystick to upper left corner and press button one...');
  97.   Repeat Until JoystickButtonA;
  98.   XMin := JoystickPosX;
  99.   YMin := JoystickPosY;
  100.   Writeln ('OK.');
  101.   Repeat Until not JoystickButtonA;
  102.   Write ('Press joystick to lower right corner and press button two...');
  103.   Repeat Until JoystickButtonB;
  104.   XMax := JoystickPosX;
  105.   YMax := JoystickPosY;
  106.   Writeln ('OK.');
  107.   Repeat Until not JoystickButtonB;
  108. end;
  109.  
  110. Procedure AnimateShip;
  111. begin
  112.   X := JoystickPosX - XMin;
  113.   if (X <= XRange div 3) then
  114.      Dec (PosX, 3)
  115.   else if (X > XRange * 2 div 3) then
  116.      Inc (PosX, 3);
  117.   Y := JoystickPosY - YMin;
  118.   if (Y <= YRange div 3) then
  119.      Dec (PosY, 3)
  120.   else if (Y > YRange * 2 div 3) then
  121.      Inc (PosY, 3);
  122.   SpriteX [0] := PosX;
  123.   SpriteY [0] := PosY;
  124. end;
  125.  
  126. Procedure AnimateBullets;
  127. begin
  128.   if Shooting1 then
  129.      if (Bullet1Y < 0) then
  130.         Shooting1 := False
  131.      else
  132.         Dec (Bullet1Y, 8)
  133.   else
  134.      begin
  135.         Bullet1X := PosX + 3;
  136.         Bullet1Y := PosY + 14;
  137.      end;
  138.   if Shooting2 then
  139.      if (Bullet2Y < 0) then
  140.         Shooting2 := False
  141.      else
  142.         Dec (Bullet2Y, 8)
  143.   else
  144.      begin
  145.         Bullet2X := PosX + 30;
  146.         Bullet2Y := PosY + 14;
  147.      end;
  148.   SpriteX [1] := Bullet1X;
  149.   SpriteY [1] := Bullet1Y;
  150.   SpriteX [2] := Bullet2X;
  151.   SpriteY [2] := Bullet2Y;
  152. end;
  153.  
  154. begin
  155.   if JoystickExists and (LoadSprite ('SHIP1.COD', 1) = 1) and
  156.      (LoadSprite ('BULLET.COD', 2) = 1) then
  157.      begin
  158.         ClrScr;
  159.         CalibrateJoystick (XMin, XMax, YMin, YMax);
  160.         ClrScr;
  161.         InitGraph;
  162.         SpriteN [0] := 1;
  163.         SpriteN [1] := 2;
  164.         SpriteN [2] := 2;
  165.         PosX := 160;
  166.         PosY := 160;
  167.         Shooting1 := False;
  168.         XRange := XMax - XMin;
  169.         YRange := YMax - YMin;
  170.         ShootNext := Boolean (0);
  171.         While not (JoystickButtonA and JoystickButtonB) do
  172.            begin
  173.               if JoystickButtonA and not JoystickButtonB then
  174.                  if not Shooting1 and ShootNext then
  175.                     begin
  176.                        Bullet1X := PosX + 3;
  177.                        Bullet1Y := PosY + 14;
  178.                        Shooting1 := True;
  179.                        ShootNext := False;
  180.                     end
  181.                  else if not Shooting2 and not ShootNext then
  182.                     begin
  183.                        Bullet2X := PosX + 30;
  184.                        Bullet2Y := PosY + 14;
  185.                        Shooting2 := True;
  186.                        ShootNext := True;
  187.                     end;
  188.               While JoystickButtonA do
  189.                  begin
  190.                     AnimateShip;
  191.                     AnimateBullets;
  192.                     Animate;
  193.                  end;
  194.               AnimateShip;
  195.               AnimateBullets;
  196.               Animate;
  197.            end;
  198.         CloseRoutines;
  199.      end
  200.   else
  201.      Writeln ('Game card not installed.');
  202. end.
  203.  
  204. I apologize For giving you an example that Uses another Unit.  if need be, this
  205. Program can be easily modified to provide a successful example.  Hope this
  206. helps, and I hope my Programming is not toO bad.
  207. }